﻿using System.Globalization;
using Pixeldyne.Core.Text;

namespace Pixeldyne.Core.Extensions
{
	/// <summary>
	/// Utility extension methods.
	/// </summary>
	public static class Numbers
	{
		/// <summary>  
		/// Returns <c>true</c> if integer value is between a range of values (start, end).  
		/// </summary>  
		/// <returns></returns>  
		public static bool Between(this long value, long start, long end, bool inclusive)
		{
			return inclusive ? value >= start && value <= end : value > start && value < end;
		}

		/// <summary>
		/// Returns <c>true</c> if integer value is between a range of values (start, end).
		/// </summary>
		/// <param name="value">The string value.</param>
		/// <param name="start">The start index.</param>
		/// <param name="end">The end index.</param>
		/// <returns></returns>
		public static bool Between(this long value, long start, long end)
		{
			return value.Between(start, end, true);
		}

		public static bool Between(this int value, int start, int end, bool inclusive)
		{
			return inclusive ? value >= start && value <= end : value > start && value < end;
		}

		public static bool Between(this int value, int start, int end)
		{
			return value.Between(start, end, true);
		}

		public static string FormatLargeNumber(int number)
		{
			return FormatLargeNumber((decimal)number);
		}

		public static string FormatLargeNumber(decimal number)
		{
			string formatted = string.Empty;

			const ulong million = 1000000;
			const ulong billion = million * 1000;
			const ulong trillion = billion * 1000;

			if (number > trillion)
			{
				number = number / trillion;
				formatted = number.ToString("F") + " Trillion";
			}
			if (number > billion)
			{
				number = number / billion;
				formatted = number.ToString("F") + " Billion";
			}
			else if (number > million)
			{
				number = number / million;
				formatted = number.ToString("F") + " Million";
			}

			if (formatted.IsEmpty())
			{
				return number.ToString(CultureInfo.InvariantCulture);
			}

			return formatted + (number > 1 ? "s" : "");
		}
	}
}